import click
from sqlalchemy.orm import sessionmaker
from models import init_db, Material, Process, Product, ProductMaterial, ProductProcess

Session = sessionmaker(bind=init_db())

@click.group()
def cli():
    """产品组价管理系统"""
    pass

# 材料管理命令组
@cli.group()
def material():
    """材料管理"""
    pass

@material.command()
@click.option('--name', prompt='材料名称', help='材料名称')
@click.option('--specification', prompt='规格', help='规格')
@click.option('--brand', prompt='品牌', help='品牌')
@click.option('--model', prompt='型号', help='型号')
@click.option('--unit', prompt='单位', help='单位')
@click.option('--price', prompt='单价', type=float, help='单价')
def add_material(name, specification, brand, model, unit, price):
    """添加新材料"""
    session = Session()
    material = Material(
        name=name,
        specification=specification,
        brand=brand,
        model=model,
        unit=unit,
        price=price
    )
    session.add(material)
    session.commit()
    click.echo(f'成功添加材料：{name}')
    session.close()

@material.command()
def list_materials():
    """列出所有材料"""
    session = Session()
    materials = session.query(Material).all()
    for m in materials:
        click.echo(f'ID: {m.id}, 名称: {m.name}, 规格: {m.specification}, '
                  f'品牌: {m.brand}, 型号: {m.model}, 单位: {m.unit}, 单价: {m.price}')
    session.close()

# 工序管理命令组
@cli.group()
def process():
    """工序管理"""
    pass

@process.command()
@click.option('--name', prompt='工序名称', help='工序名称')
@click.option('--unit', prompt='计价单位', help='计价单位')
@click.option('--price', prompt='单价', type=float, help='单价')
@click.option('--description', prompt='工序描述', help='工序描述')
def add_process(name, unit, price, description):
    """添加新工序"""
    session = Session()
    process = Process(
        name=name,
        unit=unit,
        price=price,
        description=description
    )
    session.add(process)
    session.commit()
    click.echo(f'成功添加工序：{name}')
    session.close()

@process.command()
def list_processes():
    """列出所有工序"""
    session = Session()
    processes = session.query(Process).all()
    for p in processes:
        click.echo(f'ID: {p.id}, 名称: {p.name}, 单位: {p.unit}, '
                  f'单价: {p.price}, 描述: {p.description}')
    session.close()

# 产品管理命令组
@cli.group()
def product():
    """产品管理"""
    pass

@product.command()
@click.option('--name', prompt='产品名称', help='产品名称')
@click.option('--category', prompt='产品类别', help='产品类别')
@click.option('--specification', prompt='规格', help='规格')
def create_product(name, category, specification):
    """创建新产品"""
    session = Session()
    product = Product(
        name=name,
        category=category,
        specification=specification
    )
    session.add(product)
    session.commit()
    click.echo(f'成功创建产品：{name}，ID：{product.id}')
    session.close()

@product.command()
@click.option('--product-id', prompt='产品ID', type=int, help='产品ID')
@click.option('--material-id', prompt='材料ID', type=int, help='材料ID')
@click.option('--quantity', prompt='用量', type=float, help='用量')
def add_material_to_product(product_id, material_id, quantity):
    """为产品添加材料"""
    session = Session()
    material = session.query(Material).get(material_id)
    if not material:
        click.echo('材料不存在')
        session.close()
        return
    
    product = session.query(Product).get(product_id)
    if not product:
        click.echo('产品不存在')
        session.close()
        return
    
    product_material = ProductMaterial(
        product_id=product_id,
        material_id=material_id,
        quantity=quantity,
        subtotal=material.price * quantity
    )
    session.add(product_material)
    
    # 更新产品材料成本
    product.material_cost = sum(pm.subtotal for pm in product.materials)
    product.total_price = product.material_cost + product.labor_cost
    session.commit()
    click.echo(f'成功添加材料到产品，小计：{product_material.subtotal}')
    session.close()

@product.command()
@click.option('--product-id', prompt='产品ID', type=int, help='产品ID')
@click.option('--process-id', prompt='工序ID', type=int, help='工序ID')
@click.option('--quantity', prompt='工序数量', type=float, help='工序数量')
def add_process_to_product(product_id, process_id, quantity):
    """为产品添加工序"""
    session = Session()
    process = session.query(Process).get(process_id)
    if not process:
        click.echo('工序不存在')
        session.close()
        return
    
    product = session.query(Product).get(product_id)
    if not product:
        click.echo('产品不存在')
        session.close()
        return
    
    product_process = ProductProcess(
        product_id=product_id,
        process_id=process_id,
        quantity=quantity,
        subtotal=process.price * quantity
    )
    session.add(product_process)
    
    # 更新产品人工成本
    product.labor_cost = sum(pp.subtotal for pp in product.processes)
    product.total_price = product.material_cost + product.labor_cost
    session.commit()
    click.echo(f'成功添加工序到产品，小计：{product_process.subtotal}')
    session.close()

@product.command()
@click.option('--product-id', prompt='产品ID', type=int, help='产品ID')
def show_product(product_id):
    """显示产品详细信息"""
    session = Session()
    product = session.query(Product).get(product_id)
    if not product:
        click.echo('产品不存在')
        session.close()
        return
    
    click.echo(f'\n产品信息：')
    click.echo(f'名称：{product.name}')
    click.echo(f'类别：{product.category}')
    click.echo(f'规格：{product.specification}')
    
    click.echo('\n材料清单：')
    for pm in product.materials:
        click.echo(f'- {pm.material.name}: {pm.quantity}{pm.material.unit}, '
                  f'单价：{pm.material.price}, 小计：{pm.subtotal}')
    
    click.echo('\n工序清单：')
    for pp in product.processes:
        click.echo(f'- {pp.process.name}: {pp.quantity}{pp.process.unit}, '
                  f'单价：{pp.process.price}, 小计：{pp.subtotal}')
    
    click.echo(f'\n成本汇总：')
    click.echo(f'材料成本：{product.material_cost}')
    click.echo(f'人工成本：{product.labor_cost}')
    click.echo(f'总价：{product.total_price}')
    session.close()

if __name__ == '__main__':
    cli()